package com.andon.microcellsimulator.mqtt;

import android.text.TextUtils;

import com.andon.microcellsimulator.common.mqtt.MQTTCallback;
import com.andon.microcellsimulator.common.util.JsonUtil;
import com.andon.microcellsimulator.common.util.LogUtil;

/**
 * Created by admin on 17/12/14.
 */
public abstract class MQTTMessageCallBack implements MQTTCallback {

    @Override
    public void getMessage(String msg) {
        LogUtil.i("MQTT接收到消息:[" + msg + "]");
        getSrcMessage(msg);

        if (msg.startsWith("JS01")) {
            String jsonContent = msg.substring(4);
            BaseMessage baseMessage = JsonUtil.toObj(jsonContent, BaseMessage.class);

            if (baseMessage != null) {
                if (TextUtils.equals("cmd", baseMessage.getType())) {
                    IssuedCommandMessage commandMessage = JsonUtil.toObj(jsonContent, IssuedCommandMessage.class);
                    issuedCommand(commandMessage);
                } else if (TextUtils.equals("bind", baseMessage.getType())) {
                    BindCommandMessage commandMessage = JsonUtil.toObj(jsonContent, BindCommandMessage.class);
                    bindCommand(commandMessage);
                } else {
                    LogUtil.w("未支持的MQTT消息类型:" + baseMessage.getType());
                }
            }

        }
    }

    // 获取mqtt消息原始数据
    public abstract void getSrcMessage(String msg);
    // 下发指令
    public abstract void issuedCommand(IssuedCommandMessage msg);
    // 设备绑定/解绑
    public abstract void bindCommand(BindCommandMessage msg);

}
